package service.impl;

import dao.UserLocationDao;
import domain.UserLocation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.ServiceResponse;
import service.UserLocationService;

import java.util.Date;

/**
 * Created by aceyin on 15/7/9.
 */
@Service
public class UserLocationServiceImpl extends ServiceBase implements UserLocationService {

    @Autowired
    private UserLocationDao userLocationDao;

    @Override
    @Transactional
    public ServiceResponse update(long userId, UserLocation location) {
        UserLocation db = userLocationDao.get(userId);

        String province = location.getProvince();
        db.setProvince(province);

        String city = location.getCity();
        if (StringUtils.isNotBlank(city)) {
            db.setCity(city);
        }
        String district = location.getDistrict();
        if (StringUtils.isNotBlank(district)) {
            db.setDistrict(district);
        }
        String street = location.getStreet();
        if (StringUtils.isNotBlank(street)) {
            db.setStreet(street);
        }

        db.setLatitude(location.getLatitude());
        db.setLongitude(location.getLongitude());

        db.setUpdateTime(new Date());

        userLocationDao.update(db);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse get(long userid) {
        UserLocation location = userLocationDao.get(userid);
        return ServiceResponse.success().with("location", location);
    }
}
